/*
 * LDIR
 *
 * LDIR, a GPS cartography application built for Let's Do It Romania eco
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 *
 * http://generasoftware.com/
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include "Logger.h"

#ifdef __LOG__

Logger* Logger::iLogger = NULL;
const TInt KBufferSize = 1024;

Logger::~Logger()
    {
    iLogFile.Close();
    iFs.Close();
    iBuffer.Close();
    iLogger = NULL;
    }

Logger::Logger()
    {

    }

Logger* Logger::InstanceL()
    {
    if ( iLogger )
        return iLogger;

    iLogger = new ( ELeave ) Logger;
    return iLogger;
    }

void Logger::SetSettingsL( TUint aVerbosity, const TDesC& aFilename )
    {
    iVerbosity = aVerbosity;
    User::LeaveIfError( iFs.Connect() );
    if ( !BaflUtils::FileExists( iFs, aFilename ) )
        {
        BaflUtils::EnsurePathExistsL( iFs, aFilename );
        }
    else
        {
        BaflUtils::DeleteFile( iFs, aFilename );
        }

    User::LeaveIfError( iLogFile.Create( iFs, aFilename, EFileWrite
        | EFileShareAny ) );
    iBuffer.CreateL( KBufferSize );
    }

/// Requires the KLogFile constant to be defined.
///
void Logger::LogTextL( const TDesC& aText )
    {
    TBuf<32> timeText;
    TTime currentTime;
    currentTime.HomeTime();
    currentTime.FormatL( timeText,
        _L("%D%M%Y%/0%1%/1%2%/2%3%/3 %-B%:0%J%:1%T%:2%S%:3%+B") );
    timeText += KDesSpace;

    iBuffer.Zero();
    iBuffer.Append( timeText );
    TInt left = iBuffer.MaxLength() - iBuffer.Length() - 2;
    if ( left < aText.Length() )
        {
        iBuffer.ReAllocL( KBufferSize + aText.Length() - left );
        }
    iBuffer.Append( aText.Left( iBuffer.MaxLength() - iBuffer.Length() - 2 ) );
    iBuffer.Append( KDesCRLF );

    iLogFile.Write( iBuffer );
    iLogFile.Flush();
    if ( iBuffer.MaxLength() > KBufferSize )
        {
        iBuffer.Close();
        iBuffer.CreateL( KBufferSize );
        }
    }

void Logger::LogTextL( const TDesC8& aText )
    {
    HBufC* text = HBufC::NewLC( aText.Length() );
    text->Des().Copy( aText );
    LogTextL( *text );
    CleanupStack::PopAndDestroy( text );
    }

void Logger::Log( TInt aVerbosity, TRefByValue<const TDesC> adFormat, ... )
    {
    if ( iVerbosity & aVerbosity )
        {
        VA_LIST args;
        TBuf<2048> tmpBuf;
        VA_START(args, adFormat);
        tmpBuf.AppendFormatList( adFormat, args, this );
        VA_END( args );
        TRAPD(err, LogTextL(tmpBuf));
        }
    }

void Logger::Log( TInt aVerbosity, TRefByValue<const TDesC8> aFormat, ... )
    {
    if ( iVerbosity & aVerbosity )
        {
        VA_LIST args;
        TBuf8<2048> tmpBuf;
        VA_START(args, aFormat);
        tmpBuf.AppendFormatList( aFormat, args, this );
        VA_END(args);
        TRAPD(err, LogTextL(tmpBuf));
        }
    }

void Logger::Overflow( TDes8& aDes )
    {

    }

void Logger::Overflow( TDes16& aDes )
    {

    }

#endif // __LOG__
